//
//  ZZBaseUtil.swift
//  QMXK
//
//  Created by jenkin on 2018/12/5.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit
import AlamofireImage

class ZZBaseUtil: NSObject {

    static let shareUtil:ZZBaseUtil = ZZBaseUtil.init()
    func getEmptyImage() -> UIImage{
        return UIImage.init(named: "empty_photo")!
    }
    
    func setImageView(imageView:UIImageView, url:String?){
        let picurl:URL! = URL.init(string:url ?? "")
        if picurl == nil{
            imageView.image = self.getEmptyImage()
        }else{
        
            imageView.af_setImage(withURL: picurl, placeholderImage: self.getEmptyImage())
//            imageView.af_setImage(withURL: picurl, placeholderImage: self.getEmptyImage(), filter: nil, progress: nil, progressQueue: DispatchQueue.main, imageTransition: kCATransition.noTransition, runImageTransitionIfCached: true, completion: nil)
        }
    }
    
    func saveTypeName(data:Dictionary<String, Any>?){
        let name:String! = data?["name"] as! String
        let user:UserDefaults = UserDefaults.standard
        var typeDict:Dictionary? = user.value(forKey: "typeList") as? Dictionary<String, Any>
        if typeDict == nil{
            typeDict = Dictionary.init()
        }
        typeDict?[name] = data as AnyObject
        user.set(typeDict, forKey: "typeList")
        user.synchronize()
    }
    
    func getTypeName() -> Dictionary<String, Any>?{
        let user:UserDefaults = UserDefaults.standard
        var typeList:Dictionary? = user.value(forKey: "typeList") as? Dictionary<String, Any>
        if typeList == nil{
            typeList = Dictionary.init()
        }
        return typeList
    }
    
    func saveIpHost(ipText:String){
        let user:UserDefaults = UserDefaults.standard
        user.set(ipText, forKey: "ipText")
        user.synchronize()
    }
    
    func getIpHost()->String{
        let user:UserDefaults = UserDefaults.standard
        var ipText:String? = user.value(forKey: "ipText") as? String
        return ipText ?? ""
    }
    
    func getFloatWihDatStr(text: String) -> CGFloat{
        let double = Float(text)
        let float = CGFloat(double ?? 0)
        return float
    }
    
    func getStrWithFloat(text: CGFloat) -> String{
        let double = Float(text)
        let str = String(double)
        return str
    }
    
    func saveCellW(type:Int){
        let user:UserDefaults = UserDefaults.standard
        user.set(type, forKey: "cellW")
        user.synchronize()
    }
    
    func getCellW() -> Int{
        let user:UserDefaults = UserDefaults.standard
        let type:Int! = (user.value(forKey: "cellW") as? Int) ??  0
        return type * 10 + 10
    }
}
